package ckw.gui.wizards;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import ckw.database.VotingCard;
import ckw.database.VotingSubcard;

public class WzdElectionDlgVotingSubcardAddEdit extends Dialog {
	private VotingCard votingCard;
	private VotingSubcard votingSubcard;
	
	private Text txtNazwa;
	private Text txtOpis;
	private Spinner spinner;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param votingCard 
	 * @param votingSubcard 
	 */
	public WzdElectionDlgVotingSubcardAddEdit(Shell parentShell, VotingCard votingCard, VotingSubcard votingSubcard) {
		super(parentShell);
		
		this.votingCard = votingCard;
		this.votingSubcard = votingSubcard;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(2, false));

		Label lblNazwa = new Label(container, SWT.NONE);
		lblNazwa.setText("Nazwa:");

		txtNazwa = new Text(container, SWT.BORDER);
		txtNazwa.setText(votingSubcard.getName() == null ? "" : votingSubcard.getName());
		txtNazwa.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblOpis = new Label(container, SWT.NONE);
		lblOpis.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1,
				1));
		lblOpis.setText("Opis:");

		txtOpis = new Text(container, SWT.BORDER | SWT.WRAP | SWT.MULTI);
		txtOpis.setText(votingSubcard.getDescription() == null ? "" : votingSubcard.getDescription());
		GridData gd_txtOpis = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_txtOpis.heightHint = 95;
		txtOpis.setLayoutData(gd_txtOpis);

		Label lblDozwoloneWybory = new Label(container, SWT.NONE);
		lblDozwoloneWybory.setText("Dozwolone wybory:");

		spinner = new Spinner(container, SWT.BORDER);
		spinner.setSelection(votingSubcard.getAllowedChecks() == 0 ? 1 : votingSubcard.getAllowedChecks());
		GridData gd_spinner = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_spinner.widthHint = 83;
		spinner.setLayoutData(gd_spinner);
		spinner.setMaximum(-1);
		spinner.setMinimum(1);

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	@Override
	protected void okPressed() {
		votingSubcard.setName(txtNazwa.getText());
		votingSubcard.setDescription(txtOpis.getText());
		votingSubcard.setAllowedChecks(spinner.getSelection());
		// Jesli nie zawiera, to dodajemy, jesli zawiera, po prostu edytujemy
		if(!votingCard.getVotingSubcards().contains(votingSubcard)) {
			votingCard.getVotingSubcards().add(votingSubcard);
		}
		
		super.okPressed();
	}
}
